<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="my" uri="http://www.trkj.com/tr1702/tags"%>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>工作</title>
<title>Insert title here</title>
<script type="text/javascript"
	src="<c:url value="/js/jquery-easyui-1.5.5.6/jquery.min.js"  />"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery-easyui-1.5.5.6/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery-easyui-1.5.5.6/locale/easyui-lang-zh_CN.js"/>"></script>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/js/jquery-easyui-1.5.5.6/themes/default/easyui.css"/>" />
<link rel="stylesheet" type="text/css"
	href="<c:url value="/js/jquery-easyui-1.5.5.6/themes/icon.css"/>">
</head> 
<body>
	<script type="text/javascript">
		var roleFormatter = function(value,row,index){
			var edit = "", del = "", grant = "", addUser = "";
			
			<my:g id="66">edit = " <a style='margin:3px 0px;' btn='cell_btn_edit' onclick='editRole(" + row.roleId + ")'>编辑</a>";</my:g>
			<my:g id="67">del = " <a btn='cell_btn_del' onclick='delRoles(" + row.roleId + ")'>删除</a>";</my:g>
			<my:g id="68">	grant = " <a btn='cell_btn_grant' onclick='grantRole(" + row.roleId + ")'>给角色授权</a>";</my:g>
			<my:g id="69">	addUser  = " <a btn='cell_btn_adduser' onclick='addUser(" + row.roleId + ")'>添加用户</a>";</my:g>
			return edit + del + grant + addUser;
		}
		
		$(function(){
			$("#hjj-role-list-dg").datagrid({onLoadSuccess:function(){
				$("a[btn=cell_btn_del]").linkbutton({iconCls:'icon-cancel', plain:true});
				$("a[btn=cell_btn_grant]").linkbutton({iconCls:'icon-reload', plain:true});
				$("a[btn=cell_btn_adduser]").linkbutton({iconCls:'icon-add', plain:true});
				$("a[btn=cell_btn_edit]").linkbutton({iconCls:'icon-edit', plain:true});
			}});

			$("#hjj-role-list-dg-tb-add").linkbutton({onClick:function(){
				$("#hjj-role-add-dlg").dialog('open');
			}});
			
			
		});
		/* 编辑用户 */
		var editRole= function(roleId){
			$("#update_role").dialog("open");
			$("#update_role_from").form("load","is_load_role?roleId="+roleId);
		}
		/* 给角色授权 */
		var grantRole = function(roleId){
			$("#hjj-role-add-function").dialog({roleId:roleId,onOpen:function(){
				$("#hjj-function-list-tree").tree({
					method:'post',
					url:'get_functiontypes?roleId='+roleId
				});
			}});
			$('#hjj-role-add-function').dialog('open');
		}
		/* 删除用户 */
		var delRoles =function(roleId){
			$.messager.confirm('确认','您确认想要删除记录吗？',function(r){    
			    if (r){    
			    	$.post('delete_role',{roleId:roleId},function(){
						$("#hjj-role-list-dg").datagrid("reload");
					})    
			    }else{
			    	
			    }    
			});  
			
		}
		/* 刷新 */
		$("#hjj-role-list-dg-tb-reload").linkbutton({
			onClick:function(){
				$('#hjj-role-list-dg').datagrid('reload');
			}
		});
		/* 增加用户 */
		var addUser = function(roleId){
			$("#hjj-add-stain").dialog({roleId:roleId,onOpen:function(){
				$("#hjj-emppass-list-dg").datalist({
					method:'post',
					url:'see_emppassandRole?roleId='+roleId,
					singleSelect:false,
					textField:'stainName',
					valueField:'passId',
					state:'state'
				});
			}});
			$("#hjj-add-stain").dialog("open");
			
		}
		$(function(){
			
		});
		//加载成功之后判断复选框是否被选中
		$("#hjj-emppass-list-dg").datalist({
			//加载成功后
			onLoadSuccess:function(data){
				//获取所有的行
				var getRows = $("#hjj-emppass-list-dg").datalist("getRows");
				//循环判断state的值
				for(var i=0;i<getRows.length;i++){
					var rowes = getRows[i].state;
					if(rowes==1){
						$("#hjj-emppass-list-dg").datalist("checkRow",i);
					}
				}
			}
		});
	</script>
	<table id="hjj-role-list-dg" class="easyui-datagrid" 
	        data-options="url:'get_all_roles',fit:true,fitColumns:true,singleSelect:true,method:'get',border:false,
	        	toolbar:'#hjj-role-list-dg-tb'">   
	    <thead>   
	        <tr>   
	            <th data-options="field:'roleId',width:80">编号</th>   
	            <th data-options="field:'roleName',width:100">名称</th>   
	            <th data-options="field:'xxx',width:400,formatter: roleFormatter">操作</th>   
	        </tr>   
	    </thead>   
	</table>  
	<div id="hjj-role-list-dg-tb">
			<my:g id="65"><a href="#" id="hjj-role-list-dg-tb-add" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true,text:'添加角色'"></a></my:g>
			<a href="#" id="hjj-role-list-dg-tb-reload" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true,text:'刷新'"></a>
	</div>
	 
	<!-- 添加新角色的对话框 -->
	<div id="hjj-role-add-dlg" class="easyui-dialog" title="增加新角色" style="width:400px;height:150px;"   
	        data-options="iconCls:'icon-save',resizable:true,modal:true,bodyCls:'bodyCls',closed:true,
	        buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
					$('#hjj-role-add-dlg-form').form({
						onSubmit:function(params){
							return $('#hjj-role-add-dlg-form').form('validate');
						},
						success:function(v){
							v = eval('(' + v + ')');
							if(v.success){
								$('#hjj-role-add-dlg').dialog('close');
								$('#hjj-role-list-dg').datagrid('reload');
								$('#hjj-role-add-dlg-form').form('reset');
							}else{
								$.messager.alert('错误', v.message);
							}
						}
					}).submit();
				}
			},{
				text:'关闭',
				iconCls:'icon-cancel',
				handler:function(){
					$('#hjj-role-add-dlg').dialog('close');
				}
			}]">
	        <form id="hjj-role-add-dlg-form" method="post" action="add_new_role" style="margin-top:3%;margin-left:2%;">
		        <div class="line">
					<input class="easyui-textbox" id="roleName" name="roleName" data-options="label:'角色名',required:true, width:'90%'">
				</div>
			</form>
	</div>
	<div id="hjj-add-stain" class="easyui-dialog" style="width:400px;height:300px;"   
        data-options="iconCls:'icon-save',title:'添加用户',resizable:true,closed:true,modal:true,buttons:[{
				text:'保存',
				iconCls:'icon-ok', 
				handler:function(){
					$('#grant_to_form').form({
						url:'toRoleaddEmps',
						method:'post',
						onSubmit:function(params){
							var roles= $('#hjj-add-stain').dialog('options').roleId;
							params.roleId=roles;
							var passids = $('#hjj-emppass-list-dg').datalist('getSelections');
						
							if(passids.length!=0){
									for(var i = 0; i < passids.length; i ++){
										params['passIds['+ i +']'] = passids[i].passId;
										
									}
							}
						},
						success:function(v){
							var v = eval('('+v+')');
							if(v){
								$('#hjj-add-stain').dialog('close');
							}else{
								$.messager.alert('错误', '操作失败，原因：' + v.message);
							}
						}
					}).submit();
					
				}
			},{
				text:'关闭',
				iconCls:'icon-no',
				handler:function(){
				$('#hjj-add-stain').dialog('close');
			}
			}]">
			<ul id="hjj-emppass-list-dg" class="easyui-datalist" title="用户列表"
				data-options="width:'100%',height:'100%',border:false,checkbox:true ">
			</ul>    
	</div>
	<!-- 给角色授权 -->
	<div id="hjj-role-add-function" class="easyui-dialog" title="给角色授权" style="width:400px;height:500px;"   
	        data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true, buttons:[{
					text:'保存',
					iconCls:'icon-ok',
					handler:function(){
						$('#grant_to_form1').form({
							url:'is_giverolesq',
							onSubmit:function(params){
							//角色id
							var roleId = $('#hjj-role-add-function').dialog('options').roleId;
							params.roleId=roleId;
							//功能id
							var nodes = $('#hjj-function-list-tree').tree('getChecked');
							var j = 0;
							for(var i = 0; i < nodes.length; i ++){
								if(!nodes[i].children){
									params['funcIds[' + j + ']'] = nodes[i].id;
									j ++;
								}
							}
							},
							success:function(v){
								var obj = eval('(' + v + ')');
								if(obj.success){
									$('#hjj-role-add-function').dialog('close');
								}else{
									$.messager.alert('错误', '操作失败，原因：' + obj.message);
								}
							}
						}).submit();
					}
				},{
					text:'关闭',
					iconCls:'icon-cancel',
					handler:function(){
						$('#hjj-role-add-function').dialog('close');
					}
				}]"> 
			<ul id="hjj-function-list-tree" class="easyui-tree" title="权限列表" data-options="width:'100%',height:'100%',border:false,checkbox:true "> 
			</ul>  
	</div>
	<!-- 编辑角色 -->
	<div id="update_role" class="easyui-dialog" title="编辑角色" style="width:340px;height:150px;"   
        data-options="closed:true,buttons:[{
				text:'保存',
				handler:function(){
						$('#update_to_form1').form({
						url:'update_role_name',
						onSubmit:function(params){
							params['roleId'] =$('#roleId_update').textbox('getText');
							params['roleName']=$('#roleName_update').textbox('getText');
							return $(this).form('validate');
						},
						success:function(v){
							v = eval('('+v +')');
							if(v.success){
								$('#hjj-role-list-dg').datagrid('reload');
								$.messager.alert('恭喜', '修改成功');
							}else{
								$.messager.alert('错误', '对不起，操作失败，原因：' + v.message);
							}
						$.messager.progress('close');// 如果提交成功则隐藏进度条
						$('#update_role').dialog('close');
						
						}
					}).submit();
				}
			},{
				text:'关闭',
				handler:function(){
					$('#update_role').dialog('close');
				}
			}]">   
        <form id="update_role_from" method="post">   
		    <div style="margin-left:10%;margin-top:5%;">   
		        <label for="name">角色名:</label>   
		        <input id="roleName_update" class="easyui-textbox" type="text" name="roleName" data-options="required:true" />   
		    </div>  
		     <div style="display:none;">     
		        <input id="roleId_update" class="easyui-textbox" type="text" name="roleId" />   
		    </div> 
		</form>
	</div>    
	<div>
		<form id="grant_to_form"></form>
	</div>
	<div>
		<form id="grant_to_form1"></form>
	</div>
	<div>
		<form id="update_to_form1"></form>
	</div>
</body>
</html>